package ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import com.parkcar.R;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import util.BaseActivity;
import util.Util;

/**
 * Created by Administrator on 2016/3/29.
 */
public class SharePop extends BaseActivity{
    private IWXAPI api;
    private Tencent mTencent;
    private int shareType = QQShare.SHARE_TO_QQ_TYPE_DEFAULT;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.pop_share_board);
        setTitleGone(View.GONE);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参值
        p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕的1.0
        p.alpha = 1.0f; // 设置本身透明度
        p.dimAmount = 0.5f; // 设置黑暗度
        getWindow().setAttributes(p); // 设置生效
        getWindow().setGravity(Gravity.BOTTOM); // 设置靠右对齐
        api = WXAPIFactory.createWXAPI(this, "wxac2843ffa600c3d2", true);
        mTencent = Tencent.createInstance("tencent1104505320", this.getApplicationContext());
    }

    @Override
    protected void updateUI(boolean isLogin) {

    }
    public void qqShare(View view){
        Bundle bundle = new Bundle();
        bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE,"测试");
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://connect.qq.com/");
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "来自来停车");
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME,"来停车");
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"https://mmbiz.qlogo.cn/mmbiz/ic9ThkjYFwRrnGic6z6Y4ZXp320UDthv83Jh7qzLdMZKK038zpWFESXq0LovO0m6hib7G2xcV7DONmrb8Z7GmDOBw/0?wx_fmt=png");
        mTencent.shareToQQ(this, bundle , qqShareListener);
    }
    public void qzoneShare(View view){
        Bundle bundle = new Bundle();
        bundle.putString(QzoneShare.SHARE_TO_QQ_TITLE,"测试");
        bundle.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://connect.qq.com/");
        bundle.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "来自来停车");
        bundle.putString(QzoneShare.SHARE_TO_QQ_APP_NAME,"来停车");
        bundle.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        bundle.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL,"https://mmbiz.qlogo.cn/mmbiz/ic9ThkjYFwRrnGic6z6Y4ZXp320UDthv83Jh7qzLdMZKK038zpWFESXq0LovO0m6hib7G2xcV7DONmrb8Z7GmDOBw/0?wx_fmt=png");
        mTencent.shareToQzone(this, bundle , qqShareListener);
    }
    public void wxShare(View view){
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
        msg.description="来停车";
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 50, 50, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
        msg.title="来停车";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img"+String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
        finish();
    }
    public void wxcirShare(View view){
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
        msg.description="来停车";
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 50, 50, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
        msg.title="来停车";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img"+String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);finish();
    }



    IUiListener qqShareListener = new IUiListener() {
        @Override
        public void onCancel() {
                toast("授权失败");
        }
        @Override
        public void onComplete(Object response) {
            // TODO Auto-generated method stub
          toast("分享成功");
        }
        @Override
        public void onError(UiError e) {
            // TODO Auto-generated method stub
            toast("onError:"+e.errorMessage);
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_QQ_SHARE) {
            Tencent.onActivityResultData(requestCode,resultCode,data,qqShareListener);
        }
    }
}
